<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
// eslint-disable-next-line func-names
Math.easeInOutQuad = function(t, b, c, d) {
  t /= d / 2
  if (t < 1) {
    return (c * t * t) / 2 + b
  }
  t--
  return (-c * (t * (t - 2) - 1)) / 2 + b
}

// requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
const requestAnimFrame = () => callback => { window.setTimeout(callback, 1000 / 60) }

/**
 * Because it's so fucking difficult to detect the scrolling element, just move them all
 * @param {number} amount
 */
function move(amount) {
  document.documentElement.scrollTop = amount
  document.body.parentNode.scrollTop = amount
  document.body.scrollTop = amount
}

function position() {
  return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop
}

/**
 * @param {number} to
 * @param {number} duration
 * @param {Function} callback
 */
export function scrollTo(to, duration, callback) {
  const start = position()
  const change = to - start
  const increment = 20
  let currentTime = 0
  duration = (typeof (duration) === 'undefined') ? 500 : duration
  const animateScroll = () => {
    // increment the time
    currentTime += increment
    // find the value with the quadratic in-out easing function
    const val = Math.easeInOutQuad(currentTime, start, change, duration)
    // move the document.body
    move(val)
    // do the animation unless its over
    if (currentTime < duration) {
      requestAnimFrame(animateScroll)
    } else if (callback && typeof (callback) === 'function') {
      // the animation is done so lets callback
      callback()
    }
  }
  animateScroll()
}
export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    }, // 数据总数
    page: {
      type: Number,
      default: 1,
      require: true
    }, // 当前页
    limit: {
      type: Number,
      default: 10,
      require: true
    }, // 每页数量
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50]
      }
    }, // 每页数量选择
    layout: {
      type: String,
      default: 'total, prev, pager, next'
    }, // 分页功能展示
    background: {
      type: Boolean,
      default: false
    }, // 分页样式
    autoScroll: {
      type: Boolean,
      default: true
    }, // 跳页自动滑页
    hidden: {
      type: Boolean,
      default: false
    } // 是否显示分页
  },
  data() {
    return {
      currentPage: this.page,
      pageSize: this.limit
    }
  },
  computed: {
    // currentPage: {
    //   get() {
    //     return this.page
    //   },
    //   set(val) {
    //     this.$emit('update:page', val)
    //   }
    // },
    // pageSize: {
    //   get() {
    //     return this.limit
    //   },
    //   set(val) {
    //     this.$emit('update:limit', val)
    //   }
    // }
  },
  watch: {
    page() {
      this.currentPage = this.page
    }
  },
  created() {

  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    initCurrentPage() {
      this.currentPage = 1
    },
    getPage() {
      return {
        page: this.currentPage,
        limit: this.pageSize
      }
    }
  }
}
</script>

<style lang="scss">
.pagination-container {
  margin-top: 0;
  // background: #fff;
  padding: 20px 0;
  text-align: right;
  .btn-prev, .el-pager li, .btn-next{
    background-color: transparent!important;
  }
  .pagination-container.hidden {
    display: none;
  }

  .page-style {
    line-height: 32px;
    height: 32px;
    margin-top: 15px;
    font-size: 14px;
    color: #8c8c8c;
    letter-spacing: -0.43px;
  }
}

</style>
